home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / GRAPTIES / CRISTLE.LZH / DUAL.C < prev    next >
Text File  |  1985-11-03  |  5KB  |  195 lines

  1. /* dual.c - sdt - 20-OCT-1985 - create a transformation between the
  2.  *                              purple octohedron and the blue cube,
  3.  *                              to illustrate that they are dual
  4.  *                              Platonic solids.
  5.  *
  6.  * usage:  dual [filename]
  7.  * if no file is given, the formatted data will go to stdout.
  8.  * if the named file exists, program will abort.
  9.  * example:  dual x.x
  10.  *           crystal -r x.x
  11.  *
  12.  * mods 03-NOV-1985 to take advantage of the auto reverse mode (-r)
  13.  * added to crystal (the program that displays 3d movies).
  14.  */
  15.  
  16. #include "stdio.h"
  17.  
  18.    static int nlines = 9 ;
  19. /*    primitives based on octohedron; (a,b,c) -> (x,y,z) later.
  20.  */
  21.    static float a[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 0.5, 0.0 } ;
  22.    static float b[] = { 0.0, 0.0, 0.5, 1.0, 1.0, 0.5, 0.0, 0.0, 0.0, 0.0 } ;
  23.    static float c[] = { 1.0, 1.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 1.0 } ;
  24.    static int upo[] = {   2,   1,   1,   2,   1,   1,   2,   1,   1,   0 } ;
  25.  
  26. /*    primitives based on cube; (d,e,f) -> (x,y,z) later.
  27.  */
  28.    static float d[] = { 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 } ;
  29.    static float e[] = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0 } ;
  30.    static float f[] = { 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0 } ;
  31.    static int upc[] = {   2,   1,   1,   2,   1,   1,   2,   1,   1,   0 } ;
  32.  
  33. /*    the format string to use when writing x,y,z,up.
  34.  */
  35.    static char *fmt = { "%.3f %.3f %.3f %1d\n" } ;
  36.  
  37. /*    scaled to inscribe the unit sphere
  38.  */
  39.    float x[10], y[10], z[10] ;
  40.    double pi ;
  41.  
  42. main( argc, argv )
  43. int argc;
  44. char *argv[] ;
  45. {
  46.    FILE *fp ;
  47.    extern FILE *fopen() ;
  48.    extern int fclose() ;
  49.    extern double atan() ;
  50.  
  51.    pi = atan( 1.0 ) * 4.0 ;
  52.  
  53.    if( argc == 1 ) {  /* no args */
  54.       dual( stdout ) ;
  55.    }
  56.    else {
  57.       if( (fp = fopen( *++argv, "r" )) != NULL )
  58.          abort( "dual: file %s exists - will not overwrite!", *argv ) ;
  59.       if( (fp = fopen( *argv, "w" )) == NULL )
  60.          abort( "dual: can't open %s for output", *argv ) ;
  61.       dual( fp ) ;
  62.       fclose( fp ) ;
  63.    }
  64. }
  65.  
  66. dual( fp )
  67. FILE *fp ;
  68. {
  69.    int i, j ; /* loop index */
  70.    int jmax = 40 ;
  71.    extern int scale() ;
  72.    extern double cos() ;
  73.    float sigma ;
  74.  
  75.    for( j = 0 ; j < jmax ; j++ ) {
  76.       fprintf( stderr, "j=%d\n", j ) ;
  77.       sigma = (float)j / (float)jmax ;
  78.       sigma = (1.0 - cos( sigma * pi )) * 0.5 ;
  79.       octo( sigma );
  80.       scale() ;
  81.       spit( fp, upo ) ;
  82.    }
  83.  
  84.    for( j = 0 ; j < jmax ; j++ ) {
  85.       fprintf( stderr, "j=%d\n", j ) ;
  86.       sigma = 1.0 - (float)j / (float)jmax ;
  87.       sigma = (1.0 - cos( sigma * pi )) * 0.5 ;
  88.       cube( sigma ) ;
  89.       scale() ;
  90.       spit( fp, upc ) ;
  91.    }
  92. }
  93.  
  94. int scale() /* return 0 if it worked, else 1 */
  95. {
  96.    int i ;
  97.    extern double sqrt() ;
  98.    double sqrad ;  /* square of radius */
  99.    double maxsq ;  /* max of the squares */
  100.    double maxradi ; /* one over maximum squared radius */
  101.  
  102.    for( maxsq = 0.0, i = 0 ; i <= nlines ; i++ )
  103.       if( maxsq < (sqrad = x[i] * x[i] + y[i] * y[i] + z[i] * z[i]) )
  104.          maxsq = sqrad ;
  105.    if( maxsq > 0.0 ) {
  106.       maxradi = 1.0 / sqrt( maxsq ) ;
  107.       for( i = 0 ; i <= nlines ; i++ ) {
  108.          x[i] *= maxradi ;
  109.          y[i] *= maxradi ;
  110.          z[i] *= maxradi ;
  111.       }
  112.       return( 0 ) ;
  113.    }
  114.    else
  115.       return( 1 ) ;
  116. }
  117.  
  118. spit( fp, up )
  119. FILE *fp ;
  120. int *up ;
  121. {
  122.    int i ;
  123.  
  124.    fprintf( fp, "%d\n", (nlines + 1) * 8 - 1) ;
  125.    for( i = 0 ; i <= nlines ; i++ )
  126.       fprintf( fp, fmt,  x[i],  y[i],  z[i], up[i] ) ;
  127.    for( i = 0 ; i <= nlines ; i++ )
  128.       fprintf( fp, fmt, -y[i],  x[i],  z[i], up[i] ) ;
  129.    for( i = 0 ; i <= nlines ; i++ )
  130.       fprintf( fp, fmt, -x[i], -y[i],  z[i], up[i] ) ;
  131.    for( i = 0 ; i <= nlines ; i++ )
  132.       fprintf( fp, fmt,  y[i], -x[i],  z[i], up[i] ) ;
  133.    for( i = 0 ; i <= nlines ; i++ )
  134.       fprintf( fp, fmt,  x[i],  y[i], -z[i], up[i] ) ;
  135.    for( i = 0 ; i <= nlines ; i++ )
  136.       fprintf( fp, fmt, -y[i],  x[i], -z[i], up[i] ) ;
  137.    for( i = 0 ; i <= nlines ; i++ )
  138.       fprintf( fp, fmt, -x[i], -y[i], -z[i], up[i] ) ;
  139.    for( i = 0 ; i <= nlines ; i++ )
  140.       fprintf( fp, fmt,  y[i], -x[i], -z[i], up[i] ) ;
  141. }
  142.  
  143. octo( sigma )
  144. float sigma ; /* 0.0 to 1.0 */
  145. {
  146.    int i ;
  147.    float s, t ;
  148.  
  149.    for( i = 0 ; i <= nlines ; i++ ) {
  150.       x[i] = a[i] ;
  151.       y[i] = b[i] ;
  152.       z[i] = c[i] ;
  153.    }
  154.    s = sigma * 0.5 ;       /* 0.0 to 0.5 */
  155.    t = 1.0 - sigma * 0.5 ; /* 1.0 to 0.5 */
  156.  
  157.    x[0] = x[9] = s ;
  158.    z[0] = z[9] = t ;
  159.  
  160.    y[1] = s ;
  161.    z[1] = t ;
  162.  
  163.    y[3] = t ;
  164.    z[3] = s ;
  165.  
  166.    x[4] = s ;
  167.    y[4] = t ;
  168.  
  169.    x[6] = t ;
  170.    y[6] = s ;
  171.  
  172.    x[7] = t ;
  173.    z[7] = s ;
  174. }
  175.  
  176. cube( sigma )
  177. float sigma ; /* 0.0 to 1.0 */
  178. {
  179.    int i ;
  180.    float s ;
  181.  
  182.    for( i = 0 ; i <= nlines ; i++ ) {
  183.       x[i] = d[i] ;
  184.       y[i] = e[i] ;
  185.       z[i] = f[i] ;
  186.    }
  187.    s = 1.0 - sigma ; /* 1.0 to 0.0 */
  188.  
  189.    y[0] = y[7] = y[9] = s ;
  190.  
  191.    x[1] = x[3] = s ;
  192.  
  193.    z[4] = z[6] = s ;
  194. }
  195.